using System;
using System.Collections.Generic;
using PowerSearch.Core;
using System.Xml.Linq;
using System.Linq;

namespace PowerSearch.AppServices.Parser.Provider
{
	public class FlickrParser : BaseFeedParser
	{
		private const string secretKey = "2ddbc3181df5153f";
		public override string BaseUrl
		{
			get
			{
				return
					"http://api.flickr.com/services/rest/?method=flickr.photos.search&media=photos&per_page=20&sort=relevance&text={0}&api_key=b43ed368ee0c79ce254de3bb5cef2943";
			}
		}

		protected internal override IList<Feed> ParseFeed(string responseData)
		{
			var feedList = new List<Feed>();
			var xmlDoc = XDocument.Parse(responseData);
			if (xmlDoc.Root != null)
			{
				var responseAttr = xmlDoc.Root.Attribute("stat");
				if (responseAttr != null)
				{
					if (responseAttr.Value.Equals("ok", StringComparison.InvariantCultureIgnoreCase))
					{
						var xElement = xmlDoc.Root.Element("photos");
						if (xElement != null)
							xElement.Elements("photo").ToList().ForEach(photo => feedList.Add(CreateFeedItem(photo)));
					}
					else
					{
						var errRespone = xmlDoc.Root.Element("err");
						var errCode = errRespone.Attribute("code") != null
							? errRespone.Attribute("code").Value
								: string.Empty;
						var errMsg = errRespone.Attribute("msg") != null
							? errRespone.Attribute("msg").Value
								: string.Empty;
					}
				}
			}
			return feedList;

		}

		private string BuildImageUrl(string farm, string server, string id, string secret, Size size)
		{
			const string imgUrlFormat = "http://farm{0}.staticflickr.com/{1}/{2}_{3}{4}.jpg";
			var sizeSuffix = string.Empty;
			switch (size)
			{
				case Size.Smaller:
				sizeSuffix = "_m";
				break;
				case Size.Thumbnail:
				sizeSuffix = "_t";
				break;
				case  Size.Small:
				sizeSuffix = "_n";
				break;
				//TODO: other case

			}

			return string.Format(imgUrlFormat, farm, server, id, secret, sizeSuffix);
		}

		private Feed CreateFeedItem(XElement element)
		{
			var feed = new Feed();

			var photoId = element.Attribute("id") != null ? element.Attribute("id").Value : string.Empty;
			var title = element.Attribute("title") != null ? element.Attribute("title").Value : string.Empty;
			var farmId = element.Attribute("farm") != null ? element.Attribute("farm").Value : string.Empty;
			var serverId = element.Attribute("server") != null ? element.Attribute("server").Value : string.Empty;
			var secret = element.Attribute("secret") != null ? element.Attribute("secret").Value : string.Empty;
			feed.Title = title;
			feed.Description = title;
			feed.DisplayUrl = BuildImageUrl(farmId, serverId, photoId, secret, Size.Thumbnail);
			feed.ClickUrl = BuildImageUrl(farmId, serverId, photoId, secret, Size.Smaller);

			return feed;
		}
		private enum Size
		{
			Thumbnail, //t thumbnail, 100 on longest side
			Smaller, //m small, 240 on longest side
			Small, // n	small, 320 on longest side
			Medium, //-	medium, 500 on longest side
			Large, //b	large, 1024 on longest side
			Original //o original image, either a jpg, gif or png, depending on source format


		}
	}
}

